package com.ssyc.student.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.github.aakira.expandablelayout.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StNewConceptRvAdapter;
import com.ssyc.student.bean.UnitBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 学生端--金牌金概念2界面
 * Created by Administrator on 2018/5/7 0007.
 */

@Route(path = "/student/StudentNewConceptTwoActivity")
public class StudentNewConceptTwoActivity extends BaseActivity implements View.OnClickListener {
    //返回键
    private ImageView ivBack;

    //加载框
    private RelativeLayout rlLoading;

    //列表数据
    private RecyclerView rv;

    //下拉刷新控件
    private SmartRefreshLayout srf;

    //列表适配器
    private StNewConceptRvAdapter adapter;

    //适配器的数据集合
    private List<UnitBean.DataBean> oldDatas;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_new_concept_two;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //请求服务器数据
        http();

    }


    /**
     * 获取班级列表的数据
     */

    /**
     * 请求服务器数据
     */
    private void http() {

        Map<String, String> map = new HashMap<>();

        map.put("type", "1");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.GETLESSONList, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showLoadingView(rlLoading, true);
            }

            @Override
            public void onResponse(String response, int id) {
                showLoadingView(rlLoading, false);
                if (!TextUtils.isEmpty(response)) {
                    UnitBean info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, UnitBean.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "数据解析异常");
                    }
                    if (info != null) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            oldDatas = info.getData();
                            if (null != oldDatas && oldDatas.size() != 0) {

                                //添加折叠动画效果
                                for (int i = 0; i < oldDatas.size(); i++) {
                                    oldDatas.get(i).setInterpolator(Utils.createInterpolator(Utils.LINEAR_OUT_SLOW_IN_INTERPOLATOR));
                                }
                                //设置适配器
                                initLv();
                            } else {
                                Log.i("test", "服务器返回数据为空");
                            }
                        } else {
                            Log.i("test", "返回错误码是:" + info.getState());
                        }
                    }
                }
            }
        });
    }


    /**
     * 初始化页面控件
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        rlLoading = findViewById(R.id.rl_loading_view);
        rlLoading.setVisibility(View.VISIBLE);

    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, null);
    }


    /**
     * 设置列表的方法
     */
    private void initLv() {
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new StNewConceptRvAdapter(this, R.layout.student_rv_new_concept, oldDatas);
        rv.setAdapter(adapter);
    }



    /**
     * 页面控件的点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
